How to do it?:

Submission: Submit the link on Github of the assignment to Canvas


library(tidyverse)
library(gganimate)
library(gifski)
df <- read.csv("adult_census.csv")
  1. Install two packages gganimate and gifski then restart Rstudio. Using the Adult Census Income data, make an animation using geom_point and transition_states.
df %>% ggplot(aes(x = hours.per.week,
           y = age, color=race))+
  geom_point()+
  transition_states(sex)+
  labs(title = 'sex: {closest_state}')

  1. Using the Adult Census Income data, make an animation using geom_bar and transition_states.
df %>% ggplot(aes(x = hours.per.week,
           y = age, color=marital.status))+
  geom_point()+
  transition_states(sex)+
  labs(title = 'sex: {closest_state}')

  1. Use the WHO’s dataset at this link. Make a top-10 bar race by months between countries on the number of deaths by Covid 19 in 2021.
library(gapminder)
library(gganimate)
library(ggplot2)
library(tidyverse)
library(lubridate)
library(knitr)
df <- read_csv("WHO-COVID-19-global-data.csv")
df$week <- week(df$Date_reported)
d1 <- df %>% group_by(week, Country) %>% summarise(mean = mean(New_cases))
d2 <- d1 %>% group_by(week) %>% mutate(rank=rank(-mean)) 
d3 <- d2 %>% filter(rank <= 10)
a1 <- d3 %>% ggplot(aes(x=rank, y=mean, group=Country, fill=Country, label=Country)) + geom_col()+geom_text(aes(y = mean, label = Country), hjust = 1.4)+ 
    coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
    labs(title = 'Week {closest_state}', x='', y='Total Number of Positive Caeses', fill='Country')+
    theme(plot.title = element_text(hjust = 1, size = 22),
          axis.ticks.y = element_blank(),
          axis.text.y  = element_blank()) + 
    transition_states(week)+
    ease_aes("cubic-in-out")
animate(a1, nframes = 400)

  1. Make a bar race using a dataset of your own interest. You may use the dataset that we use in class (https://covidtracking.com/data/download/all-states-history.csv) but you should make a different bar racev from ones in the slides.
df <- read.csv("all-states-history.csv")
df$week <- week(df$date)
d1 <- df %>% group_by(week, state) %>% summarise(mean = mean(hospitalizedCurrently))
d2 <- d1 %>% group_by(week) %>% mutate(rank=rank(-mean)) 
d3 <- d2 %>% filter(rank <= 10)
a1 <- d3 %>% ggplot(aes(x=rank, y=mean, group=state, fill=state, label=state)) + geom_col()+
    geom_text(aes(y = mean, label = state), hjust = 1.4)+ 
    coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
    labs(title = 'Week {closest_state}', x='', y='Total Number of Positive Caeses', fill='state')+
    theme(plot.title = element_text(hjust = 1, size = 22),
          axis.ticks.y = element_blank(),
          axis.text.y  = element_blank()) + 
    transition_states(week)+
    ease_aes("cubic-in-out")
animate(a1, nframes = 400)
## Warning: Removed 27 rows containing missing values (position_stack).
## Warning: Removed 2 rows containing missing values (geom_text).
## Removed 2 rows containing missing values (geom_text).
## Removed 2 rows containing missing values (geom_text).
## Warning: Removed 8 rows containing missing values (geom_text).
## Removed 8 rows containing missing values (geom_text).
## Removed 8 rows containing missing values (geom_text).
## Removed 8 rows containing missing values (geom_text).
## Removed 8 rows containing missing values (geom_text).
## Removed 8 rows containing missing values (geom_text).
## Removed 8 rows containing missing values (geom_text).
## Removed 8 rows containing missing values (geom_text).
## Warning: Removed 10 rows containing missing values (geom_text).
## Removed 10 rows containing missing values (geom_text).
## Removed 10 rows containing missing values (geom_text).
## Removed 10 rows containing missing values (geom_text).
## Removed 10 rows containing missing values (geom_text).
## Removed 10 rows containing missing values (geom_text).
## Removed 10 rows containing missing values (geom_text).
## Removed 10 rows containing missing values (geom_text).
## Warning: Removed 9 rows containing missing values (geom_text).
## Removed 9 rows containing missing values (geom_text).
## Removed 9 rows containing missing values (geom_text).
## Removed 9 rows containing missing values (geom_text).
## Removed 9 rows containing missing values (geom_text).
## Removed 9 rows containing missing values (geom_text).
## Removed 9 rows containing missing values (geom_text).
## Removed 9 rows containing missing values (geom_text).